﻿using System;
using System.Linq;
using System.Windows.Forms;
using IGM.DataProvider;
using IGM.DataProvider.Entites;
using IGM.Framework.Commons;
using IGM.Framework.Constants;
using IGM.Presentation.IgmServiceReference;

namespace IGM.Presentation.Management
{
    public partial class StudentMarksControl : UserControl
    {
        private readonly DomainServiceClient _domainServiceClient;

        public StudentMarksControl()
        {
            InitializeComponent();

            _domainServiceClient = new DomainServiceClient();     
        }

        private void MarkStudentsControlLoad(object sender, System.EventArgs e)
        {
            LoadKindMarks();
            Dock = DockStyle.Top;
        }
        private void LoadKindMarks()
        {
            cboKindMarks.DataSource = _domainServiceClient.GetKindMarks().Cast<KindMark>().ToList();
            cboKindMarks.DisplayMember = "Name";
            cboKindMarks.ValueMember = "Id";
        }

        private void LoadStudentMarks()
        {
            if (cboKindMarks.Items.Count > 0)
            {
                dgvStudentMarks.DataSource =
                    _domainServiceClient.GetStudentMarksByKindMarkId(((KindMark)cboKindMarks.SelectedItem).Id).Cast<StudentMark>().ToList();

                DataGridViewHelper.ShowHideColumns(dgvStudentMarks, new[]
                                                                   {
                                                                       (string) typeof (Student).Properties().IsChecked,
                                                                        "IdStudent", "Mark", "IsDebt"
                                                                   });
                DataGridViewHelper.ShowIsCheck(dgvStudentMarks);
                DataGridViewHelper.ApplyCheckedAllOrUncheckAll(dgvStudentMarks, false);
                DataGridViewHelper.ChangeDgvHeaderText(dgvStudentMarks, new[] { "IsChecked", "IdStudent", "Mark", "IsDebt" },
                                                                   new[] { "Chọn", "Sinh viên", "Điểm", "Có nợ môn" }
                                                                   );

                StudentMarkCurrentRowChanged();
            }
            else
                MessageBox.Show(ConstantMessages.CanNotFindItem, @"loại điểm số");
        }

        private void StudentMarkCurrentRowChanged()
        {
            if (dgvStudentMarks.CurrentRow != null)
            {
                txtStudent.Text = dgvStudentMarks.CurrentRow.Cells["IdStudent"].Value.ToString();
                txtMark.Text = dgvStudentMarks.CurrentRow.Cells["Mark"].Value.ToString();
                ckbDebt.Checked = (bool)dgvStudentMarks.CurrentRow.Cells["IsDebt"].Value;
            }

            else
            {
                txtStudent.Text = txtMark.Text = string.Empty;
                ckbAllStudentMarks.Checked = false;
            }
        }
        private void CboKindMarksSelectedIndexChanged(object sender, EventArgs e)
        {
            LoadStudentMarks();
        }

        private void DgvStudentMarksCurrentCellChanged(object sender, EventArgs e)
        {
            StudentMarkCurrentRowChanged();
        }

        private void DgvStudentMarksCellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvStudentMarks == null ||
               dgvStudentMarks.Columns[(string)typeof(StudentMark).Properties().IsChecked] == null ||
               dgvStudentMarks.Columns[(string)typeof(StudentMark).Properties().IsChecked].Index != e.ColumnIndex)
                return;

            dgvStudentMarks[e.ColumnIndex, e.RowIndex].Value =
                !(bool)dgvStudentMarks[e.ColumnIndex, e.RowIndex].Value;

            ckbAllStudentMarks.Checked = DataGridViewHelper.AllRowsIsCheck(dgvStudentMarks);
        }

        private void CkbAllStudentMarksCheckedChanged(object sender, EventArgs e)
        {
            DataGridViewHelper.ApplyCheckedAllOrUncheckAll(dgvStudentMarks, ckbAllStudentMarks.Checked);
        }

        private void ReloadStudentMark()
        {
            LoadStudentMarks();
            StudentMarkCurrentRowChanged();
        }     
    }
}
